import { users } from '../../../server/database/schema';
import { verifyPassword } from '../../../server/utils/auth';

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  const { email, password } = body;

  const db = useDrizzle()

  const user = await db.select()
    .from(users)
    .where(eq(users.email, email))
    .limit(1);

  if (!user.length || !(await verifyPassword(password, user[0].passwordHash))) {
    throw createError({ statusCode: 401, message: 'Invalid credentials' });
  }

  // 创建会话 (实际实现需使用JWT或session)
  await setUserSession(event, { user: user[0] })

  return { success: true, user: { id: user[0].id, email: user[0].email, nickname: user[0].nickname } };
});